package com.csu.mall.gateway.service.impl;

import com.csu.mall.common.dto.MemberPrice;
import com.csu.mall.common.dto.SkuReductionTo;
import com.csu.mall.common.utils.PageUtils;
import com.csu.mall.gateway.dao.SkuFullReductionDao;
import com.csu.mall.gateway.entity.MemberPriceEntity;
import com.csu.mall.gateway.entity.SkuFullReductionEntity;
import com.csu.mall.gateway.entity.SkuLadderEntity;
import com.csu.mall.gateway.service.MemberPriceService;
import com.csu.mall.gateway.service.SkuFullReductionService;
import com.csu.mall.gateway.service.SkuLadderService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import javax.annotation.Resource;


@Service("skuFullReductionService")
public class SkuFullReductionServiceImpl extends ServiceImpl<SkuFullReductionDao, SkuFullReductionEntity> implements SkuFullReductionService {

    @Resource
    private SkuLadderService skuLadderService;

    @Resource
    private SkuFullReductionService skuFullReductionService;

    @Resource
    private MemberPriceService memberPriceService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        return null;
    }

    @Override
    public void saveSkuReduction(SkuReductionTo reductionTo) {
        //6.4）、sku的优惠、满减等信息；gulimall_sms
        // sms_sku_ladder
        SkuLadderEntity skuLadder = new SkuLadderEntity();
        BeanUtils.copyProperties(reductionTo, skuLadder);
        skuLadder.setAddOther(reductionTo.getCountStatus());
        skuLadderService.save(skuLadder);
        // sms_sku_full_reduction

        SkuFullReductionEntity skuFullReductionEntity = new SkuFullReductionEntity();
        BeanUtils.copyProperties(reductionTo, skuFullReductionEntity);
        skuFullReductionEntity.setAddOther(reductionTo.getPriceStatus());
        skuFullReductionService.save(skuFullReductionEntity);
        // sms_member_price

        List<MemberPrice> memberPrices = reductionTo.getMemberPrice();
        List<MemberPriceEntity> memberPriceEntities =  memberPrices.stream().map(item->{
            MemberPriceEntity memberPriceEntity = new MemberPriceEntity();
            memberPriceEntity.setSkuId(reductionTo.getSkuId());
            memberPriceEntity.setMemberLevelId(item.getId());
            memberPriceEntity.setMemberLevelName(item.getName());
            memberPriceEntity.setMemberPrice(item.getPrice());
            return memberPriceEntity;
        }).collect(Collectors.toList());
        memberPriceService.saveBatch(memberPriceEntities);
    }
}